package cn.com.generaldata.os.service.indicator_evaluation.prosperity_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 沿海城市生产总值可比价增长率评估指标计算类.
 */
public class CoastalCityGDPComparablePriceRateCalculateService extends IndicatorEvaluationCalculateService {

	private String GDPGrowthRateIndicatorName = "地区生产总值增长速度";

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	/**
	 * 计算沿海城市GDP可比价增速.
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double GDPComparablePriceSum = getCoastalCityGDPComparablePriceSum(indicatorAccountVO);

		Double coastalCityGDPSum = getCoastalCityLastYearGDPSum(indicatorAccountVO);

		return (GDPComparablePriceSum / coastalCityGDPSum) - 1;
	}

	/**
	 * 获得沿海城市上年GDP现价之和.
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getCoastalCityLastYearGDPSum(IndicatorAccountVO indicatorAccountVO) {
		IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		double coastalCityGDPSum = 0d;
		for (String areaId : ProvinceIndicatorConstant.CITYS) {
			vo.setAreaId(areaId);
			Double lastGDP = getGDP(vo, vo.getYear() - 1);

			coastalCityGDPSum += lastGDP;
		}
		return coastalCityGDPSum;
	}

	/**
	 * 获得沿海城市GDP可比价之和.
	 * t时期某市生产总值可比价（上年为基期）=t-1时期某市生产总值现价*（t时期某市生产总值增速+1）
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getCoastalCityGDPComparablePriceSum(IndicatorAccountVO indicatorAccountVO) {
		double coastalCityGDPComparablePriceSum = 0d;
		for (String areaId : ProvinceIndicatorConstant.CITYS) {
			IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
			vo.setAreaId(areaId);
			Double lastGDP = getGDP(vo, vo.getYear() - 1);

			Double GDPGrowthRate = getGDPGrowthRate(vo);

			coastalCityGDPComparablePriceSum += lastGDP * (GDPGrowthRate + 1);
		}
		return coastalCityGDPComparablePriceSum;
	}

	private Double getGDP(IndicatorAccountVO indicatorAccountVO, int year) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, year,
				ProvinceIndicatorConstant.GDP_INDICATOR_NAME);
		Validate.isTrue(result != null);
		return result;
	}

	private Double getGDPGrowthRate(IndicatorAccountVO indicatorAccountVO) {
		Double growthRate = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				GDPGrowthRateIndicatorName);
		Validate.isTrue(growthRate != null, "地区生产总值增长速度为0，不可以计算");
		return growthRate;
	}

}
